import asyncio
import sys
import traceback
from functools import partial

from PyQt5 import QtWidgets, QtGui, QtCore
import YqDoc
import YqRepo
import YqUi
import YqConfig
import YqDownload


def catch_exceptions(ty, value, ui_traceback):
    """
    捕获异常，并弹窗显示
    :param ty: 异常的类型
    :param value: 异常的对象
    :param ui_traceback: ui_traceback
    """
    # traceback_format = traceback.format_exception(ty, value, ui_traceback)
    # traceback_string = "".join(traceback_format)
    QtWidgets.QMessageBox.critical(None, "错误", "{}".format(value))
    ui.old_hook(ty, value, ui_traceback)


def start_download(downloader: YqDownload, download_doc: YqDoc, config: YqConfig):
    config.save_config()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(downloader.download(download_doc.checked_boxs))
    print(downloader.save_done_num)
    print(downloader.save_error_doc)


def init_config(config_init: YqConfig, ui_init):
    config_data = config_init.get_config()
    if config_data:
        if "username" in config_data:
            username = config_data["username"]
            ui_init.lineEdit_namespace.setText(username)
            yq_config.username = username
        if "token" in config_data:
            token = config_data["token"]
            ui_init.lineEdit_token.setText(token)
            yq_config.token = token
        if "doc_path" in config_data:
            doc_path = config_data["doc_path"]
            ui_init.lineEdit_doc_save.setText(doc_path)
            yq_config.doc_path = doc_path
        if "pic_path" in config_data:
            pic_path = config_data["pic_path"]
            ui_init.lineEdit_pic_save.setText(pic_path)
            yq_config.pic_path = pic_path


if __name__ == "__main__":
    # 获取UIC窗口操作权限
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = QtWidgets.QMainWindow()
    # 调自定义的界面
    ui = YqUi.Ui_MainWindow()
    ui.setupUi(mainWindow)

    # 加载配置文件
    yq_config = YqConfig.YqConfig()
    init_config(yq_config, ui)
    # 全局异常管理
    ui.old_hook = sys.excepthook
    sys.excepthook = catch_exceptions
    # 显示窗口
    mainWindow.show()

    # 初始化知识库
    repo = YqRepo.YqRepo()
    ui.Button_link.clicked.connect(partial(repo.get_repo, ui, yq_config))

    # 初始化文章列表
    doc = YqDoc.YqDoc()
    ui.Button_get_doc.clicked.connect(partial(doc.get_doc, repo, ui, True))

    # 文件、图片保存
    ui.Button_save_doc.clicked.connect(partial(yq_config.choose_doc_path, ui))
    ui.Button_save_pic.clicked.connect(partial(yq_config.choose_pic_path, ui))

    # 下载
    yq_download = YqDownload.YqDownload(repo, yq_config, ui, mainWindow)
    ui.Button_download.clicked.connect(partial(start_download, yq_download, doc, yq_config))

    # 翻页
    ui.Button_page_prev.clicked.connect(doc.get_prev_page)
    ui.Button_page_next.clicked.connect(doc.get_next_page)
    if doc.page_offset < 1:
        ui.Button_page_prev.setDisabled(True)

    # 全选
    ui.Button_select_all.clicked.connect(doc.select_all_doc)
    # 进度条
    ui.progressBar.setValue(0)
    # 进入程序的主循环
    sys.exit(app.exec_())
